﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AnalisisDeRiesgos.Models.Process;
using AnalisisDeRiesgos.Models;

namespace AnalisisDeRiesgos.Controllers
{
    public class ProcessController : Controller
    {
        //
        // GET: /Process/

        public ActionResult Index()
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.Administrator))
                return RedirectToAction("LogOn", "User");

            SearchProcessModel model = new SearchProcessModel();

            return View(model);
        }

        [HttpPost]
        public ActionResult Index(SearchProcessModel model)
        {
            if (ModelState.IsValid)
                model.SearchManagement(model.Pattern);

            return View(model);
        }

        //
        // GET: /Process/Details/5

        public ActionResult Details(int ?id)
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.Administrator))
                return RedirectToAction("LogOn", "User");

            if (id == null)
                RedirectToAction("Index");

            try
            {
                DetailsProcessModel model = new DetailsProcessModel(id.Value);

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", new ErrorModel { Message = ex.Message });
            }
        }

        //
        // GET: /Process/Create

        public ActionResult Create()
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.Administrator))
                return RedirectToAction("LogOn", "User");

            RegisterProcessModel model = new RegisterProcessModel();

            return View(model);
        } 

        //
        // POST: /Process/Create

        [HttpPost]
        public ActionResult Create(RegisterProcessModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return View();
                }

                model.Save();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                return View(model);
            }
        }
        
        //
        // GET: /Process/Edit/5
 
        public ActionResult Edit(int ?id)
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.Administrator))
                return RedirectToAction("LogOn", "User");

            if (id == null)
                return RedirectToAction("Index");

            try
            {
                EditProcessModel model = new EditProcessModel(id.Value);

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", new ErrorModel { Message = ex.Message });
            }
        }

        //
        // POST: /Process/Edit/5

        [HttpPost]
        public ActionResult Edit(EditProcessModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View(model);

                model.Save();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }

            return View(model);
        }

        //
        // GET: /Process/Delete/5
 
        public ActionResult Delete(int ?id)
        {
            if (!Utils.HasPrivilege(Convert.ToInt32(Session["userid"]), Privileges.Administrator))
                return RedirectToAction("LogOn", "User");

            if (id == null)
                return RedirectToAction("Index");
            try
            {
                DetailsProcessModel model = new DetailsProcessModel(id.Value);

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", new ErrorModel { Message = ex.Message });
            }
        }

        //
        // POST: /Process/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                DetailsProcessModel model = new DetailsProcessModel(id);
                try
                {
                    model.DeleteManagement();
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.Message);
                }

                return View(model);
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Error(ErrorModel errorMsg)
        {
            return View(errorMsg);
        }
    }
}
